let requestUrl;
let typeArray = ["野生", "宝宝", "变异"];

function addHandler() {
	requestUrl = "/chongwu_init_zizhi/insert";

	$("#zizhi_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#zizhi_list").datagrid("getSelected");
	
	if(rowData) {
		requestUrl = "/chongwu_init_zizhi/updateById";

		$("#id").val(rowData.id);
		$("#gjzz").numberspinner("setValue", rowData.gjzz);
		$("#fyzz").numberspinner("setValue", rowData.fyzz);
		$("#tlzz").numberspinner("setValue", rowData.tlzz);
		$("#flzz").numberspinner("setValue", rowData.flzz);
		$("#sdzz").numberspinner("setValue", rowData.sdzz);
		$("#chongwuId").combobox("setValue", rowData.chongwuId);
		$("#chengzhang").numberspinner("setValue", rowData.chengzhang);

		// 特殊宠物才有的字段
		$("#name").textbox("setValue", rowData.name);
		$("#fightGrade").numberspinner("setValue", rowData.fightGrade);
		$("#specialType").combobox("setValue", rowData.specialType);
		$("#improveTimes").numberspinner("setValue", rowData.improveTimes);
		$("#skillQuantity").numberspinner("setValue", rowData.skillQuantity);
		$("#neidanQuantity").numberspinner("setValue", rowData.neidanQuantity);

		$("#zizhi_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#zizhi_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/chongwu_init_zizhi/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#zizhi_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的数据！", "warning");
	}
}

$(document).ready(function() {

	$("#chongwu_id").combobox({
		url: "/chongwu_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-宠物类型-",
		panelHeight: "auto",
		formatter: function(row) {
			return "<img src='" + row.image + "' height='18' />&nbsp;" + row.name;
		}
	});

	$("#special_type").combobox({
		url: "/chongwu_zizhi_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		editable: true,
		prompt: "-特殊资质类型-",
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let specialType = $("#special_type").numberspinner("getValue");
		let chongwuId = $("#chongwu_id").combobox("getValue");

		$("#zizhi_list").datagrid("reload", {
			specialType: specialType,
			chongwuId: chongwuId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#gjzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#fyzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#tlzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#flzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#sdzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#chengzhang").numberspinner({
		min: 0,
		max: 1300,
		width: 120,
		required: true
	});

	fetchSelect("#type", typeArray, {
		width: 120
	});

	$("#chongwuId").combobox({
		url: "/chongwu_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img src='" + row.image + "' height='18' />&nbsp;" + row.name;
		}
	});

	$("#name").textbox({
		width: 120,
		required: false
	});

	$("#specialType").combobox({
		url: "/chongwu_zizhi_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		editable: true
	});

	$("#fightGrade").numberspinner({
		min: 0,
		max: 115,
		width: 120,
		editable: true
	});

	$("#improveTimes").numberspinner({
		min: 1,
		max: 6,
		width: 120,
		editable: true
	});

	$("#skillQuantity").numberspinner({
		min: 1,
		max: 5,
		width: 120,
		editable: true
	});

	$("#neidanQuantity").numberspinner({
		min: 1,
		max: 5,
		width: 120,
		editable: true
	});

	$("#zizhi_dialog").dialog({
		title: "宠物初始资质",
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#zizhi_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#zizhi_dialog").dialog("close");
						$("#zizhi_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#zizhi_form").form("clear");
				$("#zizhi_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#zizhi_list").datagrid({
		url: "/chongwu_init_zizhi/selectByPage",
		method: "get",
		height: height_table,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		frozenColumns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "gjzz", title: "攻击资质", align: "center", width: 100},
			{field: "fyzz", title: "防御资质", align: "center", width: 100},
			{field: "tlzz", title: "体力资质", align: "center", width: 100},
			{field: "flzz", title: "法力资质", align: "center", width: 100},
			{field: "sdzz", title: "速度资质", align: "center", width: 100},
			{field: "chengzhang", title: "成长", align: "center", width: 100}
		]],
		columns: [[
			{field: "type", title: "资质类型", align: "center", width: 100
				, formatter: function(value) {
					if (value != null) {
						return typeArray[value];
					}

					return "/";
				}
			},
			{field: "chongwuId", title: "宠物类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					if (value != null) {
						ajaxGet("/chongwu_category/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#chongwu_" + rowIndex).attr({
								alt: data.name,
								src: data.image,
								title: data.name
							});
						}, error);

						return "<img height='" + size + "' id='chongwu_" + rowIndex + "' />";
					} else {
						return "<img height='" + size + "' title='无' src='/images/blank.png' />";
					}
				}
			},
			{field: "name", title: "资质名称", align: "center", width: 100
				, formatter: function(value) {
					if (value != null) {
						return value;
					}

					return "/";
				}
			},
			{field: "fightGrade", title: "参战等级", align: "center", width: 100
				, formatter: function(value) {
					if (value != null) {
						return value;
					}

					return "/";
				}
			},
			{field: "improveTimes", title: "提升次数", align: "center", width: 100
				, formatter: function(value) {
					if (value != null) {
						return value;
					}

					return "/";
				}
			},
			{field: "skillQuantity", title: "技能数量", align: "center", width: 100
				, formatter: function(value) {
					if (value != null) {
						return value;
					}

					return "/";
				}
			},
			{field: "neidanQuantity", title: "内丹数量", align: "center", width: 100
				, formatter: function(value) {
					if (value != null) {
						return value;
					}

					return "/";
				}
			},
			{field: "specialType", title: "特殊资质", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					if (value != null) {
						ajaxGet("/chongwu_zizhi_type/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#special_type_" + rowIndex).html(data.name);
						}, error);

						return "<div id='special_type_" + rowIndex + "'></div>";
					}

					return "/";
				}
			}
		]]
	});

});